package com.seeyon.ctp.rest.resources;

import com.alibaba.fastjson.JSON;
import com.seeyon.apps.hems.kit.FormKit;
import com.seeyon.apps.hems.kit.ResultKit;
import com.seeyon.apps.hems.kit.StrKit;
import com.seeyon.apps.hems.manager.BatchLabelPrintingManager;
import com.seeyon.apps.hems.po.BatchLabelPrintingPO;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.log.CtpLogFactory;
import org.apache.commons.logging.Log;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.*;

@Path("jssdk")
@Consumes(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON})
public class BatchLabelPrintingResources extends BaseResource {

    private static final Log log = CtpLogFactory.getLog(BatchLabelPrintingResources.class);

    private BatchLabelPrintingManager batchLabelPrintingManager = (BatchLabelPrintingManager) AppContext.getBean("batchLabelPrintingManager");

    /**
     * 临时保存待打印数据ID
     */
    @POST
    @Path("dataIds")
    public Response addDataIds(Map<String, Object> param) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        try {
            log.info("需要打印标签数据ID集合:" + JSON.toJSONString(param));
            BatchLabelPrintingPO batchLabelPrintingPO = new BatchLabelPrintingPO();
            batchLabelPrintingPO.setIdIfNew();
            batchLabelPrintingPO.setCreateMemberId(AppContext.getCurrentUser().getId());
            batchLabelPrintingPO.setCreateTime(new Date());
            batchLabelPrintingPO.setBatchLabelPrintIds(StrKit.str(param.get("ids")).replace("[", "").replace("]", ""));

            /*将数据保存到中间表中*/
            batchLabelPrintingManager.save(batchLabelPrintingPO);
            dataMap.put("dataId", StrKit.str(batchLabelPrintingPO.getId()));
            return ok(ResultKit.getResult("S", "", dataMap));
        } catch (Exception e) {
            log.error("保存打印标签数据方法报错信息:", e);
            return ok(ResultKit.getResult("E", e.getMessage(), dataMap));
        }
    }

    /**
     * 获取标签批量打印数据信息
     *
     * @param param {"dataId":""}
     * @return
     */
    @POST
    @Path("batchLabelPrinting")
    public Response batchLabelPrinting(Map<String, Object> param) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        try {
            log.info("标签批量打印接口入参信息：" + JSON.toJSONString(param));
            String dataId = StrKit.str(param.get("dataId"));

            if ("".equals(dataId)) {
                return ok(ResultKit.getResult("E", "数据ID不能为空值!", list));
            }
            BatchLabelPrintingPO batchLabelPrintingPO = batchLabelPrintingManager.findDataById(Long.parseLong(dataId));
            if (batchLabelPrintingPO == null) {
                return ok(ResultKit.getResult("E", "数据不存在!", list));
            }
            String batchLabelPrintIds = batchLabelPrintingPO.getBatchLabelPrintIds();
            List<String> dataIds = Arrays.asList(batchLabelPrintIds.split(","));
            for (String batchLabelPrintId : dataIds) {
                Map<String, Object> dataMap = FormKit.batchLabelPrintingDatas(Long.parseLong(batchLabelPrintId.trim()));
                if (!dataMap.isEmpty()) {
                    list.add(dataMap);
                }
            }
            return ok(ResultKit.getResult("S", "", list));
        } catch (Exception e) {
            log.error("获取标签批量打印数据信息接口报错信息:", e);
            return ok(ResultKit.getResult("E", e.getMessage(), list));
        }
    }

    /**
     * 更新入库打印标签数据打印状态
     *
     * @param param {"dataId":""}
     * @return
     */
    @POST
    @Path("update/data/state")
    public Response updateBatchLabelPrintState(Map<String, Object> param) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        try {
            log.info("更新入库打印标签数据打印状态接口入参信息：" + JSON.toJSONString(param));
            String dataId = StrKit.str(param.get("dataId"));

            if ("".equals(dataId)) {
                return ok(ResultKit.getResult("E", "数据ID不能为空值!", list));
            }

            BatchLabelPrintingPO batchLabelPrintingPO = batchLabelPrintingManager.findDataById(Long.parseLong(dataId));
            if (batchLabelPrintingPO == null) {
                return ok(ResultKit.getResult("E", "数据不存在!", list));
            }
            String batchLabelPrintIds = batchLabelPrintingPO.getBatchLabelPrintIds();
            List<String> dataIds = Arrays.asList(batchLabelPrintIds.split(","));
            /*遍历数据ID*/
            for (String batchLabelPrintId : dataIds) {
                /*更新打印状态*/
                FormKit.updateBatchLabelPrintState(Long.parseLong(batchLabelPrintId.trim()));
            }
            return ok(ResultKit.getResult("S", "", list));
        } catch (Exception e) {
            log.error("获取标签批量打印数据信息接口报错信息:", e);
            return ok(ResultKit.getResult("E", e.getMessage(), list));
        }
    }

}
